/**********************************************************
    COPYRIGHT
        KND, Inc. (c) 2011
    CLASS NAME
        platform_ssp.c
    DEPENDENCY

    HISTORY
		11/28/2011, created by Guangxing Liu
**********************************************************/
#include "platform_ssp.h"
#include "platform_config.h"
#include "platform_uart.h"
#include "lpc177x_8x_ssp.h"

/************************************************************************
	FUNCTION NAME : platform_ssp_init
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		Platform ssp init
************************************************************************/
void platform_ssp_init(void)
{
	
}

/************************************************************************
	FUNCTION NAME : platform_ssp_device_open
	INPUTS: 
 		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		ssp Device Open
************************************************************************/
DeviceHandle platform_ssp_device_open(void)
{
	platform_ssp_app_call* handle = (platform_ssp_app_call*)malloc(sizeof(platform_ssp_app_call));
	if(handle)
	{
		handle->iOpt = Platform_Ssp_App_Call_None;
	}
	return (DeviceHandle)handle;
 }	

/************************************************************************
	FUNCTION NAME : platform_ssp_device_close
	INPUTS: 
		DeviceHandle handle
	OUTPUTS:	
		none
	DESCRIPTION:	
		ssp Device Close
************************************************************************/
int platform_ssp_device_close(DeviceHandle handle)
{
	platform_ssp_app_call* me = (platform_ssp_app_call*)handle;
	if(me)
	{
		free(me);
	}
	return 0;
	
}/************************************************************************
	FUNCTION NAME : platform_ssp_device_ioctl
	INPUTS: 
		DeviceHandle handle, const char *params
	OUTPUTS:	
		none
	DESCRIPTION:	
		ssp Device Control
************************************************************************/
int platform_ssp_device_ioctl(DeviceHandle handle, const char *params)
{
	platform_ssp_app_call* me = (platform_ssp_app_call*)handle;
	
	switch(*params)
	{
		case '0':
			me->iOpt = Platform_Ssp_App_Call_Ssp_Init;
			return 0;
			
		default:
			platform_printf("ERROR--platform_ssp_device_ioctl unkown params: %s\r\n",params);
			break;
	}

	return -1;
}

/************************************************************************
	FUNCTION NAME :platform_ssp_device_read
	INPUTS: 
		DeviceHandle handle, char *buf, int len
	OUTPUTS:	
		none
	DESCRIPTION:	
		ssp Device Read
************************************************************************/
int platform_ssp_device_read(DeviceHandle handle, void *buf, int len)
{
	platform_ssp_app_call* me = (platform_ssp_app_call*)handle;
	
	if(!me || !buf)
		return -1;
	switch(me->iOpt)
	{
		case 0:			
	         	return 0;
		default:
			platform_printf("ERROR--platform_ssp_device_read unkown params: %s\r\n",me->iOpt);
			break;
	}
	
	return -1;

}

/************************************************************************
	FUNCTION NAME : platform_ssp_device_write
	INPUTS: 
		DeviceHandle handle,  const char *buf, int len
	OUTPUTS:	
		none
	DESCRIPTION:	
		ssp Device Write
************************************************************************/
int platform_ssp_device_write(DeviceHandle handle,  void *buf, int len)
{
	platform_ssp_app_call* me = (platform_ssp_app_call*)handle;
	if(!me || !buf)
		return -1;
	switch(me->iOpt)
	{
		case Platform_Ssp_App_Call_Ssp_Init:
			{
				SSP_CFG_Type SSP_InitStruct;

				platform_ssp_config* ssp_config = (platform_ssp_config*)buf;

				SSP_ConfigStructInit(ssp_config->ssp_no,&SSP_InitStruct);

				switch(ssp_config->ssp_no)
				{
					case PORT_SSP0:	
						
						SSP_Init(LPC_SSP0,&SSP_InitStruct);
						
						break;
					case PORT_SSP1:
						
						SSP_Init(LPC_SSP1,&SSP_InitStruct);
						
						break;
					default:
						return -1;
				}
							
			}
			return 0;
		default:
			platform_printf("ERROR--platform_ssp_device_write unkown params: %s\r\n",me->iOpt);
			break;
	}
	return -1;

}
/************************************************************************
	FUNCTION NAME : platform_init_ssp_device
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		ssp Device Init
************************************************************************/
void platform_init_ssp_device(void)
{
	DeviceInterface ssp = {
                               platform_ssp_device_open,
                               platform_ssp_device_close,
                               platform_ssp_device_ioctl,
                               platform_ssp_device_read,
                               platform_ssp_device_write
                              };


	hyacinth_install_device(SSP_DEVICE,&ssp);
}

/************************************************************************
	FUNCTION NAME : platform_finit_ssp_device
	INPUTS: 
		none
	OUTPUTS:	
		none
	DESCRIPTION:	
		ssp Device Finit
************************************************************************/
void platform_finit_ssp_device(void)
{
	hyacinth_uninstall_device(SSP_DEVICE);
}



























